""" plugin.py

	Plugin base class. """
	
import sys, os, threading, time, sppt, datetime
	
class pluginList():
	def __init__(self):
		self.list = []
		self.rl = datetime.datetime.now()
		
	def all(self):
		return self.list
		
	def find(self, entry):
		for p in self.list:
			if p.info["name"].lower() == entry.lower():
				return p.p
		return None
		
	def reloadall(self):
		return None
		
	def count(self, type='all'):
		count = 0
		if type == 'all':
                        return len(self.list)
		for p in self.list:
                        if not hasattr(p, 'flags'):
                                continue
			if type == 'system' and ("s" in p.flags):
				count += 1
			elif type == 'standard' and not ("s" in p.flags):
				count += 1
		return count
		
	def add(self, entry):
		self.list.append( entry )
		
class protocol(threading.Thread):
	def __init__(self, source):
		self.info = {}
		self.source = source.replace(".py", "")
		self.p = None
		
		time.sleep(0.01)
		
		try:
			sppt.write("Loading protocol: %s|" % self.source)
			source = __import__(self.source)

			if hasattr(source, 'exposed') == False:
                                #A friendlier message for non-exposed script files
                                self.skip = True
                                sppt.write('Warning: source for protocol %s was not loaded because it has no exposed class.' % self.source)
                                del source
                                return None
                        elif source.exposed == None:
                                self.skip = True
                                #exposed variable specifically set to None, so we know it was not a mistake
                                #on the part of script writer. This should not be loaded as a plugin.
                                del source
                                return None
			self.p = source.exposed()
		except:
			self.skip = True
			sppt.write( "Warning: source for protocol %s could not be loaded:" % self.source )
			for line in sys.exc_info():
				sppt.write( "   * " + str(line) )
			raise
			try:
				del source
			except: pass
			return None

		try:
			#test for error
			e = str(self.p)
		except:
			self.skip = True
			sppt.write( "Warning: %s error in protocol [%s]:" % (str(sys.exc_info()[0]), self.source) )
			for error in sys.exc_info():
				sppt.write( "   * " + str(error) )
			raise
			return None
			
	def Invoke(self, event, params):
		self.p.Invoke(event, params)

		
class plugin(threading.Thread):
	def __init__(self, source):
		self.info = {}
		self.source = source.replace(".py", "")
		self.p = None
		
		time.sleep(0.01)
		
		try:
			source = __import__(self.source)
			if hasattr(source, 'exposed') == False:
                                #A friendlier message for non-exposed script files
                                self.skip = True
                                sppt.write('Warning: source for %s was not loaded because it has no exposed class.' % self.source)
                                del source
                                return None
                        elif source.exposed == None:
                                self.skip = True
                                #exposed variable specifically set to None, so we know it was not a mistake
                                #on the part of script writer. This should not be loaded as a plugin.
                                del source
                                return None
			self.p = source.exposed()
			if (hasattr(self.p), "__attr__"):
				self.info = self.p.__attr__()
			else:
				self.info = {"name":"unknown", "version":0.0, "author":"unknown"}
		except:
			self.skip = True
			sppt.write( "Warning: source for %s could not be loaded:" % self.source )
			for line in sys.exc_info():
				sppt.write( "   * " + str(line) )
			try:
				del source
			except: pass
			return None

		try:
			support.doprint( "Loaded %s %s by %s." % (self.info["name"],
                                                                 '.'.join(map(str, self.info["version"])),
                                                                  self.info["author"]) )
		except:
			self.skip = True
			sppt.write( "Warning: %s error in file [%s]:" % (str(sys.exc_info()[0]), self.source) )
			for error in sys.exc_info():
				sppt.write( "   * " + str(error) )
			return None
